layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

	let year = new Date().getFullYear();//年
	let month = new Date().getMonth() + 1;//注意！月份是从0月开始获取的，所以要+1;

    laydate.render({
        elem: '#startTime',
 		format: 'yyyy-MM',
		type:'month',
		value:year + '-' + (month >= 10 ? month : '0' + month)
    });

    /**
     * 调度单表管理
     */
    var TDispatch = {
        tableId: "tDispatchTable"
    };

	var _data = []
	reds(year + '-' + (month >= 10 ? month : '0' + month));
	
	function reds(month){
		$.ajax({
			type: 'get',
			data: {month:month},
			url: "/panel/w6tms",
			async: false,
			dataType:"json",
			success: function (data) {
				if(data.data.length == 0){
    				yer.msg('未查询到数据'); 
					return
				}
				_data = []
				let arr = $.parseJSON(data.data).result
				var operate = $("#operate").val()
				
				for (var j = 0; j < arr[6].YDXLCBFDFX.length ; j++) {
					var obj1 = {}
				    if(operate != '全部'){
						if(arr[6].YDXLCBFDFX[j].GSMC.indexOf(operate) != -1){
							obj1.type = "均方"
							obj1.xianlu = arr[6].YDXLCBFDFX[j].XL
							obj1.yi = ''
							obj1.er = ''
							obj1.san = ''
							obj1.si = ''
							obj1.wu = ''
							obj1.liu = ''
							var ckj = (arr[6].YDXLCBFDFX[j].CKJ*1).toFixed(2)
							var pjj = (arr[6].YDXLCBFDFX[j].PJJ*1).toFixed(2)
							obj1.ckj = ckj
							obj1.pjj = pjj
							var chayi = (ckj-pjj).toFixed(2)
							if(chayi >= -10 && chayi <= 10){
								obj1.yi = chayi
							}
							if(chayi <= -11 && chayi >= -20){
								obj1.er = chayi
							}
							if(chayi >= 11 && chayi <= 20){
								obj1.er = chayi
							}
							if(chayi <= -21 && chayi >= -30){
								obj1.san = chayi
							}
							if(chayi >= 21 && chayi <= 30){
								obj1.san = chayi
							}
							if(chayi <= -31 && chayi >= -40){
								obj1.si = chayi
							}
							if(chayi >= 31 && chayi <= 40){
								obj1.si = chayi
							}
							if(chayi <= -41 && chayi >= -50){
								obj1.wu = chayi
							}
							if(chayi >= 41 && chayi <= 50){
								obj1.wu = chayi
							}
							if(chayi <= -51){
								obj1.liu = chayi
							}
							if(chayi >= 51){
								obj1.liu = chayi
							}
							_data.push(obj1)
						}
				    } else {
						obj1.type = "均方"
						obj1.xianlu = arr[6].YDXLCBFDFX[j].XL
						obj1.yi = ''
						obj1.er = ''
						obj1.san = ''
						obj1.si = ''
						obj1.wu = ''
						obj1.liu = ''
						var ckj = (arr[6].YDXLCBFDFX[j].CKJ*1).toFixed(2)
						var pjj = (arr[6].YDXLCBFDFX[j].PJJ*1).toFixed(2)
						obj1.ckj = ckj
						obj1.pjj = pjj
						var chayi = (ckj-pjj).toFixed(2)
						if(chayi >= -10 && chayi <= 10){
							obj1.yi = chayi
						}
						if(chayi <= -11 && chayi >= -20){
							obj1.er = chayi
						}
						if(chayi >= 11 && chayi <= 20){
							obj1.er = chayi
						}
						if(chayi <= -21 && chayi >= -30){
							obj1.san = chayi
						}
						if(chayi >= 21 && chayi <= 30){
							obj1.san = chayi
						}
						if(chayi <= -31 && chayi >= -40){
							obj1.si = chayi
						}
						if(chayi >= 31 && chayi <= 40){
							obj1.si = chayi
						}
						if(chayi <= -41 && chayi >= -50){
							obj1.wu = chayi
						}
						if(chayi >= 41 && chayi <= 50){
							obj1.wu = chayi
						}
						if(chayi <= -51){
							obj1.liu = chayi
						}
						if(chayi >= 51){
							obj1.liu = chayi
						}
						_data.push(obj1)
				    }
				}
				
				for (var j = 0; j < arr[7].YDXLCBFDFX_DC.length ; j++) {
					var obj1 = {}
				    if(operate != '全部'){
						if(arr[7].YDXLCBFDFX_DC[j].GSMC.indexOf(operate) != -1){
							obj1.type = "吨车"
							obj1.xianlu = arr[7].YDXLCBFDFX_DC[j].XL
							obj1.yi = ''
							obj1.er = ''
							obj1.san = ''
							obj1.si = ''
							obj1.wu = ''
							obj1.liu = ''
							var ckj = (arr[7].YDXLCBFDFX_DC[j].CKJ*1).toFixed(2)
							var pjj = (arr[7].YDXLCBFDFX_DC[j].PJJ*1).toFixed(2)
							obj1.ckj = ckj
							obj1.pjj = pjj
							var chayi = (ckj-pjj).toFixed(2)
							if(chayi >= -10 && chayi <= 10){
								obj1.yi = chayi
							}
							if(chayi <= -11 && chayi >= -20){
								obj1.er = chayi
							}
							if(chayi >= 11 && chayi <= 20){
								obj1.er = chayi
							}
							if(chayi <= -21 && chayi >= -30){
								obj1.san = chayi
							}
							if(chayi >= 21 && chayi <= 30){
								obj1.san = chayi
							}
							if(chayi <= -31 && chayi >= -40){
								obj1.si = chayi
							}
							if(chayi >= 31 && chayi <= 40){
								obj1.si = chayi
							}
							if(chayi <= -41 && chayi >= -50){
								obj1.wu = chayi
							}
							if(chayi >= 41 && chayi <= 50){
								obj1.wu = chayi
							}
							if(chayi <= -51){
								obj1.liu = chayi
							}
							if(chayi >= 51){
								obj1.liu = chayi
							}
							_data.push(obj1)
						}
				    } else {
						obj1.type = "吨车"
						obj1.xianlu = arr[7].YDXLCBFDFX_DC[j].XL
						obj1.yi = ''
						obj1.er = ''
						obj1.san = ''
						obj1.si = ''
						obj1.wu = ''
						obj1.liu = ''
						var ckj = (arr[7].YDXLCBFDFX_DC[j].CKJ*1).toFixed(2)
						var pjj = (arr[7].YDXLCBFDFX_DC[j].PJJ*1).toFixed(2)
						obj1.ckj = ckj
						obj1.pjj = pjj
						var chayi = (ckj-pjj).toFixed(2)
						if(chayi >= -10 && chayi <= 10){
							obj1.yi = chayi
						}
						if(chayi <= -11 && chayi >= -20){
							obj1.er = chayi
						}
						if(chayi >= 11 && chayi <= 20){
							obj1.er = chayi
						}
						if(chayi <= -21 && chayi >= -30){
							obj1.san = chayi
						}
						if(chayi >= 21 && chayi <= 30){
							obj1.san = chayi
						}
						if(chayi <= -31 && chayi >= -40){
							obj1.si = chayi
						}
						if(chayi >= 31 && chayi <= 40){
							obj1.si = chayi
						}
						if(chayi <= -41 && chayi >= -50){
							obj1.wu = chayi
						}
						if(chayi >= 41 && chayi <= 50){
							obj1.wu = chayi
						}
						if(chayi <= -51){
							obj1.liu = chayi
						}
						if(chayi >= 51){
							obj1.liu = chayi
						}
						_data.push(obj1)
				    }
				}
			}
		})
	
    
	    /**
	     * 初始化表格的列
	     */
	    TDispatch.initColumn = function () {
	        return [[
	            {type: 'checkbox',rowspan:2},
	            {field: 'type', title: '线路类型' ,rowspan:2, align: "center"},
	            {field: 'xianlu', title: '线路名称' ,rowspan:2, align: "center"},
	            {field: 'ckj', title: '参考价' ,rowspan:2, align: "center"},
	            {field: 'pjj', title: '平均价' ,rowspan:2, align: "center"},
	            {field: '', title: '参考价-平均价',colspan:6, align: "center"},
	        ],[
	            {field: 'yi', title: '-10 - 10', align: "center", sort: true},
	            {field: 'er', title: '-11 - -20 & 11-20', align: "center", sort: true},
	            {field: 'san', title: '-21 - -30 & 21-30', align: "center", sort: true},
	            {field: 'si', title: '-31 - -40 & 31-40', align: "center", sort: true},
	            {field: 'wu', title: '-41 - -50 & 41-50', align: "center", sort: true},
	            {field: 'liu', title: '-51以下 & 51以上', align: "center", sort: true}
	        ]];
	    };
	    
	    

	    // 渲染表格
	    var tableResult = table.render({
	        elem: '#' + TDispatch.tableId,
	        data:_data,
	        page: false,
	        limit: 1000,
	        height: "full-60",
	        totalRow: true,
	        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
	        defaultToolbar: ['filter', 'exports', 'print'],
	        cellMinWidth: 100,
	        cols: TDispatch.initColumn()
	    });
	}

    /**
     * 导出excel按钮
     */
    TDispatch.exportExcel = function () {
        var checkRows = table.checkStatus(TDispatch.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 导出excel
    $('#btnExp').click(function () {
        TDispatch.exportExcel();
    });

    /**
     * 点击查询按钮
     */
    TDispatch.search = function () {
		reds($("#startTime").val())
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TDispatch.search();
    });

    // 单击行事件
    table.on('row(' + TDispatch.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + TDispatch.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + TDispatch.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });
    
});
